package com.bw.usercenter.adapter

import android.content.Context
import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import com.bw.usercenter.R
import android.widget.TextView
import android.widget.Toast
import com.bw.mvpcore.glideload.GlidImg
import com.bw.usercenter.adapter.RecyclerviewAdapter.OnItemClickListener
import com.bw.usercenter.model.protocol.resp.RespSearchEntity

class RecyclerviewAdapter(var context: Context,var data : List<RespSearchEntity.Data>):
    RecyclerView.Adapter<RecyclerviewAdapter.ViewHoder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoder {
        var inflate:View = LayoutInflater.from(context).inflate(R.layout.finditem, null)
        return ViewHoder(inflate)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    inner class ViewHoder(itemView: View): RecyclerView.ViewHolder(itemView), View.OnClickListener {
        var name:TextView
        var money:TextView
        var img:ImageView
        init {
            name=itemView.findViewById(R.id.find_name)
            money = itemView.findViewById(R.id.find_money)
            img = itemView.findViewById(R.id.find_img)

        }

        override fun onClick(v: View?) {
            if (listener!=null) {
                listener?.onItemClick(v!!,position)
            }
        }

    }

    override fun onBindViewHolder(holder: ViewHoder, position: Int) {
        holder.name.text = data.get(position).Title
        holder.money.text = data.get(position).ZkFinalPrice.toString()
        GlidImg.glidImg.getimg(context,holder.img,data.get(position).PictUrl)
        holder.itemView.setTag(position)
        holder.img.setOnClickListener{
            listener?.onItemClick(holder.img,position)
        }
    }
    var listener: OnItemClickListener?=null

    interface OnItemClickListener{
        fun onItemClick(v:View,int: Int)
    }

    fun setOnItemClickListener(listen: OnItemClickListener){
        this.listener = listen
    }

    /*override fun onClick(v:View){
        var positi =v.getTag() as Int
        if (listener!=null){
            when(v.id){
                R.id.find_img -> listener?.onItemClick(v,positi)
            }
        }
    }*/


}